package edu.service.impl;

import edu.service.CourseService;
import edu.vo.Course;
import edu.repository.CourseRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CourseServiceImpl implements CourseService {

    private final CourseRepository courseRepository;

    public CourseServiceImpl(CourseRepository courseRepository) {
        this.courseRepository = courseRepository;
    }

    @Override
    public Course saveCourse(Course course) {
        return courseRepository.save(course);
    }

    @Override
    public Optional<Course> getCourseById(Integer id) {
        return courseRepository.findById(id);
    }

    @Override
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }

    @Override
    public void deleteCourseById(Integer id) {
        courseRepository.deleteById(id);
    }

    @Override
    public Course updateCourse(Integer id, Course course) {
        return courseRepository.findById(id)
                .map(existingCourse -> {
                    existingCourse.setStartDate(course.getStartDate());
                    existingCourse.setEndDate(course.getEndDate());
                    existingCourse.setClassHour(course.getClassHour());
                    existingCourse.setTestMode(course.getTestMode());
                    existingCourse.setStudentNum(course.getStudentNum());
                    existingCourse.setChoiceNum(course.getChoiceNum());
                    existingCourse.setComplete(course.getComplete());
                    existingCourse.setTeacher(course.getTeacher());
                    existingCourse.setBaseCourse(course.getBaseCourse());
                    existingCourse.setCourseType(course.getCourseType());
                    existingCourse.setScoreDeadline(course.getScoreDeadline());
                    existingCourse.setScoreStatus(course.getScoreStatus());
                    existingCourse.setScoreRatios(course.getScoreRatios());
                    existingCourse.setScores(course.getScores());
                    return courseRepository.save(existingCourse);
                })
                .orElse(null);
    }
}